package com.nondo.rdp.auth.repository;

import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.domain.UserDto;
import com.nondo.rdp.core.common.StatusConstant;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 用户DAO接口
 *
 * @author slacrey
 * @version 2013-01-15
 */
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    @Query("select u from User u where u.username=:username and delFlag = '" + StatusConstant.DEL_FLAG_NORMAL + "'")
    Optional<User> findByUsername(@Param("username") String username);

    @Modifying
    @Query("update User set delFlag='" + StatusConstant.DEL_FLAG_DELETE + "' where id = ?1")
    int deleteById(Long id);

    @Modifying
    @Query("update User set password=?1 where id = ?2")
    int updatePasswordById(String newPassword, Long id);

    @Modifying
    @Query("update User set loginIp=?1, loginDate=?2 where id = ?3")
    int updateLoginInfo(String loginIp, Date loginDate, Long id);

    Optional<User> findByPhone(String phone);

    @Query("select u from User u where delFlag = '" + StatusConstant.DEL_FLAG_NORMAL + "'")
    List<User> getAll();

    int countByInviterId(long id);
}